{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import math\n",
      "import numpy as np\n",
      "from utilsP4 import *\n",
      "%matplotlib inline \n",
      "\n",
      "# Carreguem les dades de la borsa mitja\u00e7ant la funci\u00f3 loadStockData\n",
      "data={}\n",
      "#companies=['GOOG','MSFT','IBM','YHOO','FB']\n",
      "companies=['YHOO']\n",
      "for c in companies:\n",
      "    data[c]=loadStockData(c)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercici 1"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<b>La finestra lliscant:</b>\n",
      "Les finestres lliscants s\u00f3n l'eina m\u00e9s comuna per treballar sobre fluxos de dades. Donat que els fluxos de dades s\u00f3n virutalment infinits el que usualment es fa \u00e9s considerar el conjunt de dades finit de mostres anteriors a la que ens arriba al temps actual. \n",
      "\n",
      "<b>Exemple:</b> Considerem la finestra lliscant de mida W=8. Observeu que en verd es troben identificades les posicions que \u00e9s consideren per fer els c\u00e0lculs en el temps t i blau les posicions que \u00e9s consideren en el temps t+1.\n",
      "<table border=\"1\">\n",
      "<tr>\n",
      "<td>0.22</td>\n",
      "<td>0.88</td>\n",
      "<td>0.21</td>\n",
      "<td>0.41</td>\n",
      "<td>0.33</td>\n",
      "<td>0.41</td>\n",
      "<td>0.12</td>\n",
      "<td>0.43</td>\n",
      "<td>0.38</td>\n",
      "<td>0.22</td>\n",
      "\n",
      "</tr>\n",
      "<tr>\n",
      "<td></td>\n",
      "<td  bgcolor=\"#00FF00\">t-W+1 </td>\n",
      "<td  bgcolor=\"#00FF00\"></td>\n",
      "<td  bgcolor=\"#00FF00\"></td>\n",
      "<td  bgcolor=\"#00FF00\"></td>\n",
      "<td  bgcolor=\"#00FF00\"></td>\n",
      "<td  bgcolor=\"#00FF00\">t-2</td>\n",
      "<td  bgcolor=\"#00FF00\">t-1</td>\n",
      "<td  bgcolor=\"#00FF00\">t</td>\n",
      "<td ></td>\n",
      "\n",
      "</tr>\n",
      "<tr>\n",
      "<td></td>\n",
      "<td></td>\n",
      "<td  bgcolor=\"#33FFFF\">t-W+2</td>\n",
      "<td  bgcolor=\"#33FFFF\"></td>\n",
      "<td  bgcolor=\"#33FFFF\"></td>\n",
      "<td  bgcolor=\"#33FFFF\"></td>\n",
      "<td  bgcolor=\"#33FFFF\"></td>\n",
      "<td  bgcolor=\"#33FFFF\">t-1</td>\n",
      "<td  bgcolor=\"#33FFFF\">t</td>\n",
      "<td  bgcolor=\"#33FFFF\">t+1</td>\n",
      "</tr>\n",
      "</table>\n",
      "\n",
      "Amb aquesta consideraci\u00f3 es pot entendre que realitzar c\u00e0lculs sobre fluxos de dades t\u00e9 la seva dificultat i pot ser impossible de trobar solucions exactes sense emmagatzemar tot el flux. Tot i aix\u00f2 hi ha c\u00e0lculs que s\u00ed es poden realitzar de forma exacta nom\u00e9s considerant les dades que ens arriben. Un d'aquests c\u00e0lculs \u00e9s el de la mitjana aritm\u00e8tica.\n",
      "$$ \\bar{x} = \\frac{1}{W} \\sum_{i=t-W+1}^{t}x_i$$\n",
      "\n",
      "Observeu que en l'exemple anterior la mitjana en el temps t t\u00e9 un valor 0.39625 i la mitjana en el temps t+1 t\u00e9 un valor 0.31375.\n",
      "\n",
      "Com podeu apreciar a la formula de la mitjana aritm\u00e8tica, necessiteu guardar tot el flux de dades de la finestra t per el c\u00e0lcul de la mitjana en el temps t+1. <b> Penseu e implementeu un m\u00e8tode que ens permeti treure la mitjana en el temps t+1 a partir de la mitjana en el temps t.</b>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class SlidWin:\n",
      "    \"\"\"\n",
      "        Finestra lliscant que permet trobar la mitja d'una s\u00e8rie de dades de mida m\u00e0xima W_size.\n",
      "        Aquesta finestra lliscant recalcula la mitja amb les f\u00f2rmules d'increment i decrement que\n",
      "        es troben m\u00e9s abaix d'aquest fitxer.\n",
      "    \"\"\"\n",
      "    def __init__(self, W_size):\n",
      "        \"\"\"\n",
      "            Constructor que inicialitza els par\u00e0metres per la finestra lliscant.\n",
      "            param W_size Mida m\u00e0xima de la finestra lliscant.\n",
      "        \"\"\"\n",
      "        self.data = [] # dades\n",
      "        self.W_size = W_size # tamany de la finestra\n",
      "        self.length = 0 # length actual de la finestra\n",
      "        self.m = 0.0 # mitja de la finestra\n",
      "        \n",
      "    def add(self, x_t):\n",
      "        \"\"\"\n",
      "            Afegeix una nova dada a la finestra i retorna la mitja d'aquesta i el valor de l'accio:.\n",
      "            return \tnew_mean,  1  : si la mitja ha disminuit\n",
      "\t\t\t\t\tnew_mean, -1  : si la mitja ha augmentat\n",
      "        \"\"\"\n",
      "\n",
      "        if self.length < self.W_size:\n",
      "            self.length += 1\n",
      "        else:\n",
      "            self.data.remove(self.data[0])\n",
      "\n",
      "        self.data.append(x_t)\n",
      "        m = np.mean(self.data)\n",
      "\n",
      "        action = 0\n",
      "        if (m < self.m):\n",
      "            action = 1\n",
      "        else:\n",
      "            action = -1\n",
      "\n",
      "        self.mean = m\n",
      "\n",
      "        return self.mean, action"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# PROVA AMB DADES SINTETIQUES\n",
      "\n",
      "# Sliding window amb un tamany de finestra ==10\n",
      "method1 = SlidWin(10)\n",
      "# Sliding window amb un tamany de finestra ==50\n",
      "method2 = SlidWin(50)\n",
      "\n",
      "v=[] # Genera un mostra de 400 + 600 valors amb un canvi brusc entre 400 i 401.\n",
      "for i in xrange(100):\n",
      "    v.append(0.6+0.1*(random.random()-0.5))\n",
      "for i in xrange(200):\n",
      "    v.append(0.4+0.1*(random.random()-0.5))\n",
      "   \n",
      "# Anem afegint de forma seq\u00fcencial les dades dins la nostra finestra lliscant. Guardem \n",
      "# el resultat dins la llista output1 i output2\n",
      "output1=[]\n",
      "output2=[]\n",
      "for item in v:\n",
      "    mean,action=method1.add(item)\n",
      "    output1.append(mean)\n",
      "    mean,action=method2.add(item)\n",
      "    output2.append(mean)\n",
      "    \n",
      "  \n",
      "# Visualitzem el resultat\n",
      "pylab.plot(v)\n",
      "pylab.plot(output1,'r') # plot del resultat del M\u00e8tode 1\n",
      "pylab.plot(output2,'g') # plot del resultat del M\u00e8tode 2\n",
      "pylab.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Prova amb dades de la Borsa utilizant l'estrat\u00e8gia b\u00e0sica que us donem\n",
      "method = SlidWin(10)\n",
      "for item in data:\n",
      "    # Creem un objecte broker. Aquest objecte cada cop que entrem una nova dada dira si hem de comprar o vendre accions\n",
      "    # per_change indica el percentatge de canvi respecte al valor de l'accio per realitzar una compra/venta\n",
      "    # min_time indica el temps minim que ens hem d'esperar per fer nova accio de compra/venta\n",
      "    per_change=0.1\n",
      "    min_time=10\n",
      "    broker= StockMarketWin(method,per_change,min_time)\n",
      "    # Executem l'estrtegia de comprar a partir de l'objecte broker i les dades d'entrada. La funci\u00f3 estrategiaBasica \n",
      "    # est\u00e0 definidia dins utilsP4.py. Ella \u00e9s la responsable de decidir la quantatit de compra o venta d'accios a partir de la\n",
      "    # suggeriencia del broker\n",
      "    temp_badget,invested_money,non_strategy= estrategiaBasica(broker,data[item])\n",
      "    # Mostrem els resultats per pantalla\n",
      "    print_results(data[item],temp_badget,invested_money,non_strategy)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercici 2"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "En aquest exercici heu de definir l'algoritme de la finestra adaptativa (<b>AdWin</b>). Us donem l'estructura de la classe. Aix\u00ed com en la finestra lliscant, tenim el constructor i el m\u00e8tode add. La vostra tasca consisteix en implementar el m\u00e8tode <b>add</b>. Aquest m\u00e8tode, tant a la classe anterior com en aquesta, serveix per afegir una nova dada a la finestra, en el cas de la finestra adaptativa, haurem de veure si hi ha hagut un canvi estad\u00edsticament significatiu dins les dades o no. \n",
      "La informaci\u00f3 detallada de l'algortime la podeu trobar a les p\u00e0gines 30-31 de la lli\u00e7o <b>Processament de Seq\u00fc\u00e8ncies</b> de les classes te\u00f2riques.\n",
      "<br>Aquest m\u00e8tode ha de retornar :\n",
      "<ul>\n",
      "<li>0 si no hi ha hagut cap canvi significatiu</li>\n",
      "<li>1 si hi ha hagut un canvi i la mitja ha disminu\u00eft</li>\n",
      "<li>-1 si hi ha hagut un canvi i la mitja ha augmentat</li>\n",
      "</ul>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import sqrt, log\n",
      "\n",
      "class AdWin:\n",
      "    \"\"\"\n",
      "        Finestra lliscant adaptativa que permet trobar la mitja de totes les dades que es van inserin i que permet trobar canvis bruscos\n",
      "        en elles i adaptar-se a ells a partir d'un valor de confian\u00e7a indicat per l'usuari.\n",
      "    \"\"\"\n",
      "    def __init__(self, d):\n",
      "        \"\"\"\n",
      "            Constructor de la finestra.\n",
      "            \n",
      "            param d Confian\u00e7a el canvi. Com m\u00e9s gran m\u00e9s f\u00e0cil ser\u00e0 detectar un canvi.\n",
      "        \"\"\"\n",
      "        self.data = []\n",
      "        self.length = 0\n",
      "        self.rel = d\n",
      "        self.m = 0.0\n",
      "        \n",
      "    def add(self, x_t):\n",
      "        \"\"\"\n",
      "            Afegeix una nova dada a la finestra i despr\u00e9s de comprovar si hi ha hagut un canvi actualitza les dades necess\u00e0ries.\n",
      "\n",
      "            param x_t Nova dada.\n",
      "\n",
      "            return self.m Mitja de les dades del interior de la finestra\n",
      "            return \t0 si no hi ha hagut cap canvi significatiu\n",
      "                    1 si hi ha hagut un canvi i la mitja ha disminu\u00eft\n",
      "                    -1 si hi ha hagut un canvi i la mitja ha augmentat\n",
      "        \"\"\"\n",
      "        # afegim la nova dada a m\n",
      "        self.data.append(x_t)\n",
      "        self.length += 1\n",
      "        action = 0\n",
      "        \n",
      "        # comprobem que tenim almenys dues dades\n",
      "        if self.length > 1:\n",
      "\n",
      "            # fem un recorregut mentre no es compleixi la condicio abs(mean(esquerra) - mean(dreta)) <= ecut\n",
      "\n",
      "            for i in range(1,self.length):\n",
      "                #: part esquerra\n",
      "                w_0 = self.data[:i]\n",
      "                #: part dreta\n",
      "                w_1 = self.data[i:]\n",
      "\n",
      "                # calculem m\n",
      "                m = 1.0 / ( (1.0 / len(w_0))  + (1.0 / len(w_1)) )\n",
      "\n",
      "                rel_prima = self.rel / float(len(w_0) + len(w_1))\n",
      "                    \n",
      "                # calculem ecut\n",
      "                ecut = sqrt( (1.0 / (2 * m)) * log(4.0 / rel_prima) )\n",
      "                \n",
      "                # si la diferencia es mes gran que ecut, eliminem la cua\n",
      "                if abs(mean_funct(w_0) - mean_funct(w_1)) >= ecut:\n",
      "                    self.m = mean_funct(w_0)\n",
      "                    # eliminem l'ultima part\n",
      "                    self.data = w_1[:]\n",
      "                    # disminuim la mida\n",
      "                    self.length = len(self.data)\n",
      "                    # sortim del for pero tornar a particionar\n",
      "                    break\n",
      "\n",
      "\n",
      "        #print self.data\n",
      "        new_mean = mean_funct(self.data)\n",
      "        if new_mean < self.m:\n",
      "            action = 1\n",
      "        elif new_mean > self.m:\n",
      "            action = -1\n",
      "            \n",
      "        self.m = new_mean\n",
      "        \n",
      "        return self.m, action\n",
      "\n",
      "\n",
      "def mean_funct(lista):\n",
      "\n",
      "    return sum(lista) / float(len(lista))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# PROVA AMB DADES SINTETIQUES\n",
      "\n",
      "# Adatptative window amb d=0.98\n",
      "method1 = AdWin(0.95)\n",
      "\n",
      "v=[] # Genera un mostra de 400 + 600 valors amb un canvi brusc entre 400 i 401.\n",
      "for i in xrange(200):\n",
      "    v.append(0.6+0.1*(random.random()-0.5))\n",
      "for i in xrange(400):\n",
      "    v.append(0.4+0.1*(random.random()-0.5))\n",
      "for i in xrange(600):\n",
      "    v.append(0.1*(random.random()-0.5))\n",
      "    \n",
      "# Anem afegint de forma seq\u00fcencial les dades dins la nostra finestra lliscant. Guardem el resultat dins la llista output1 i output2\n",
      "output1=[]\n",
      "for item in v:\n",
      "    mean,action=method1.add(item)\n",
      "    output1.append(mean)\n",
      "    \n",
      "  \n",
      "# Visualitzem el resultat\n",
      "pylab.plot(v)\n",
      "pylab.plot(output1,'r') # plot del resultat del M\u00e8tode 1\n",
      "pylab.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAAEACAYAAABRQBpkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztnXl8VcX5/983uTdkA8KOJEGQBBIEIpi4L7EUYyjEBb8a\nqWgtVVxwq1K3tkJbFeryQ6VV7NcdBdraL7hARIpR60JUVKyIIhKMYV8S1mw35/fHMDnnnnvuktyE\nbM/79crrbHPmzCTwfGaeeWbGZRiGgSAIgtCpiWrtAgiCIAitj4iBIAiCIGIgCIIgiBgIgiAIiBgI\ngiAIiBgIgiAINIMYFBUVkZGRQXp6OnPmzPF7/tBDDzF69GhGjx7NyJEjcbvdVFRURPpZQRAEoRlx\nRTLPwOv1MmzYMFauXElycjI5OTksXLiQzMxMx/Svv/46c+fOZeXKlU0usCAIgtD8RNQzKCkpIS0t\njUGDBuHxeCgsLGTp0qUB07/88stcdtllkXxSEARBaAEiEoPy8nJSU1MbrlNSUigvL3dMe+jQId58\n800mTZoUyScFQRCEFiAiMXC5XGGnfe211zjjjDNISkqK5JOCIAhCC+CO5OXk5GTKysoarsvKykhJ\nSXFMu2jRoqAuorS0NDZu3BhJcQRBEDoVQ4YM4bvvvm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aFRUVHap+c+bMMYYPH26MGDHCuOKKK4yampp2Xb/CwkLjmGOOMTwej5GSkmI8\n88wzTarPJ598YowYMcIYMmSIceONN7ZGVRyx1+/pp5820tLSjIEDBzbYl+uuu64hfXPVTyadCYIg\nCK0/6UwQBEFofUQMBEEQBBEDQRAEQcRAEARBQMRAEARBQMRAEARBQMRAEARBQMRAEARBAP4/LPNI\nLVNd81sAAAAASUVORK5CYII=\n",
       "text": [
        "<matplotlib.figure.Figure at 0x3683c90>"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Prova amb dades de la Borsa utilizant l'estrat\u00e8gia b\u00e0sica que us donem\n",
      "method = AdWin(0.95)\n",
      "for item in data:\n",
      "    # Creem un objecte broker. Aquest objecte cada cop que entrem una nova dada dira si hem de comprar o vendre accions\n",
      "    # per_change indica el percentatge de canvi respecte al valor de l'accio per realitzar una compra/venta\n",
      "    # min_time indica el temps minim que ens hem d'esperar per fer nova accio de compra/venta\n",
      "    broker= StockMarketWin(method,0.1,10)\n",
      "    # Executem l'estrtegia de comprar a partir de l'objecte broker i les dades d'entrada. La funci\u00f3 estrategiaBasica \n",
      "    # est\u00e0 definidia dins utilsP4.py. Ella \u00e9s la responsable de decidir la quantatit de compra o venta d'accios a partir de la\n",
      "    # suggeriencia del broker\n",
      "    temp_badget,invested_money,non_strategy= estrategiaBasica(broker,data[item])\n",
      "    # Mostrem els resultats per pantalla\n",
      "    print_results(data[item],temp_badget,invested_money,non_strategy)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Exercici 3"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Us hem donat implementada una estrat\u00e8gia de compra/venta d'accions de borsa molt b\u00e0sica ( funci\u00f3 <b>estrategiaBasica</b> de l'arxiu <b>utilsP4.py</b>). L'estrat\u00e8gia de la funci\u00f3 \u00e9s la seg\u00fcent:\n",
      "<ul><li>Gastem el 25% dels diners amb noves accions quant el broker ens diu COMPRAR i venem el 25% de les accions que tenim quant el broker ens diu VENDRE</li></ul>\n",
      "\n",
      "Penseu i implementeu una nova estrat\u00e8gia de compra/venta d'accions. Per definir la vostra estrat\u00e8gia us heu de limitar a utilitzar: \n",
      "<ul>\n",
      "<li>L'acci\u00f3 recomanada en l'estat actual.</li>\n",
      "<li>L'\u00faltima acci\u00f3 presa.</li>\n",
      "<li>El valor de la variaci\u00f3 de la mitja.</li>\n",
      "</ul>\n",
      "Compareu els resultats amb l'estrat\u00e8gia b\u00e0sica. Sou capa\u00e7os de guanyar diners? S\u00f3n importants els par\u00e0metres del AdWin? Quin efecte produeixen?"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Carreguem les dades de la borsa mitja\u00e7ant la funci\u00f3 loadStockData\n",
      "data={}\n",
      "#companies=['GOOG','MSFT','IBM','YHOO','FB']\n",
      "companies=['IBM']\n",
      "for c in companies:\n",
      "    data[c]=loadStockData(c)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def estrategia(broker,data,badget=100000.00):\n",
      "    \"\"\"\n",
      "        Estr\u00e0tegia de joc. \n",
      "        Parametres d'entrada : StockMarketWin \n",
      "                             : Dades - dades que utilizarem per fer el test\n",
      "                             : badget- quantitat de diners que volem jugar\n",
      "    \"\"\"\n",
      "    # Definim els parametres inicials de l'estrategia\n",
      "    # Gastem el 50% del badget en accions\n",
      "    n_stocks= math.floor((badget*0.25)/float(data[0]))\n",
      "    init_stocks=n_stocks\n",
      "    # Descomptem del badget el valor de les acciones que hem comprat\n",
      "    badget = badget - n_stocks * float(data[0])\n",
      "    \n",
      "    # inicialitzem variables\n",
      "    invested_money=np.zeros(len(data))\n",
      "    temp_badget=np.zeros(len(data))\n",
      "    non_strategy=np.zeros(len(data))\n",
      "    \n",
      "    # definimos 3 ventanas slidwin\n",
      "    # Utilizaremos un sistema tendencial con 3 medias moviles\n",
      "    # que trabajaran sobre los datos aportados por el AdWin\n",
      "    # Sliding window amb un tamany de finestra ==13 para corto plazo\n",
      "    slid_corto = SlidWin(13)\n",
      "    # Sliding window amb un tamany de finestra ==40 para medio plazo\n",
      "    slid_medio = SlidWin(40)\n",
      "    # Sliding window amb un tamany de finestra ==200 para largo plazo\n",
      "    slid_largo = SlidWin(200)\n",
      "    # guardaremos sus valores para representarlos graficamente\n",
      "    output_corto = []\n",
      "    output_medio = []\n",
      "    output_largo = []\n",
      "    \n",
      "    cont = 0\n",
      "    # contadores\n",
      "    compra = 0\n",
      "    venta = 0\n",
      "    \n",
      "    # definimos un valor minimo de iteraciones para realizar una accion de compra o venta\n",
      "    confianza = 13\n",
      "    \n",
      "    \n",
      "    for current_value in data:\n",
      "        # afegim una nova dada i obtenim l'accio recomanada pel broker\n",
      "        # no lo tenemos en cuenta\n",
      "        action  = broker.add(float(current_value))\n",
      "        \n",
      " \n",
      "        # recuperamos los valores de las medias a corto, medio y largo plazo\n",
      "        mean_corto,action_corto=slid_corto.add(float(current_value))\n",
      "        mean_medio,action_medio=slid_medio.add(float(current_value))\n",
      "        mean_largo,action_largo=slid_largo.add(float(current_value))\n",
      "        \n",
      "        # guardamos los datos para visualizarlos\n",
      "        output_corto.append(mean_corto)\n",
      "        output_medio.append(mean_medio)\n",
      "        output_largo.append(mean_largo)\n",
      "        \n",
      "        # accion compra \n",
      "        # si el valor a corto esta por encima del valor a medio\n",
      "        # y el valor a medio esta por encima del valor a largo\n",
      "        # para obtener la tendencia\n",
      "        if mean_corto > mean_medio and mean_medio > mean_largo:\n",
      "            venta = 0\n",
      "            compra += 1\n",
      "            if compra >= confianza:\n",
      "                \n",
      "                primera_compra = 0.25\n",
      "                # compramos un 1% del badget que tenemos\n",
      "                #money2spend = (primera_compra / compra) * badget\n",
      "                money2spend = (primera_compra) * badget\n",
      "                new_actions = math.floor(money2spend/float(current_value))\n",
      "                n_stocks += new_actions\n",
      "                badget -= new_actions*float(current_value)\n",
      "                # ponemos el contador a 0\n",
      "                compra = 0\n",
      "                \n",
      "            \n",
      "        # accion venta\n",
      "        # Si el valor a corto esta por debajo del valor a medio\n",
      "        # y el valor a medio esta por debajo del valor a largo\n",
      "        # para obtener la tendencia\n",
      "        if mean_corto < mean_medio and mean_medio < mean_largo:\n",
      "            compra = 0\n",
      "            venta += 1\n",
      "            if venta >=confianza:\n",
      "                primera_venta = 0.25\n",
      "                # vendemos un 1% de nuestras acciones\n",
      "                factor = primera_venta #/ venta\n",
      "                \n",
      "                actions2sell=round(factor * n_stocks)\n",
      "                \n",
      "                n_stocks-= actions2sell\n",
      "                badget+=actions2sell*float(current_value)\n",
      "                venta = 0\n",
      "                    \n",
      "\n",
      "                \n",
      "        temp_badget[cont]=badget\n",
      "        invested_money[cont]=n_stocks*float(current_value)\n",
      "        non_strategy[cont]=(badget/2) + init_stocks*float(current_value)\n",
      "        cont = cont+1\n",
      "\n",
      "    \n",
      "    # Visualitzem el resultat\n",
      "    pylab.plot(data, linewidth=1)\n",
      "    pylab.plot(output_corto,'r') # plot del resultado de corto plazo\n",
      "    pylab.plot(output_medio,'g') # plot del resultado de medio plazo\n",
      "    pylab.plot(output_largo,'c') # plot del resultado de largo plazo\n",
      "    pylab.legend(['Entrada', 'Corto', 'Medio', 'Largo'],bbox_to_anchor=(1.05, 1),loc=2,borderaxespad=0.)\n",
      "    pylab.show()\n",
      "\n",
      "    return temp_badget,invested_money,non_strategy"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "method = AdWin(0.95)\n",
      "for item in data:\n",
      "    # Creem un objecte broker. Aquest objecte cada cop que entrem una nova dada dira si hem de comprar o vendre accions\n",
      "    # per_change indica el percentatge de canvi respecte al valor de l'accio per realitzar una compra/venta\n",
      "    # min_time indica el temps minim que ens hem d'esperar per fer nova accio de compra/venta\n",
      "    broker= StockMarketWin(method,0.1,10)\n",
      "    # Executem l'estrtegia de comprar a partir de l'objecte broker i les dades d'entrada. La funci\u00f3 estrategiaBasica \n",
      "    # est\u00e0 definidia dins utilsP4.py. Ella \u00e9s la responsable de decidir la quantatit de compra o venta d'accios a partir de la\n",
      "    # suggeriencia del broker\n",
      "    temp_badget,invested_money,non_strategy= estrategia(broker,data[item])\n",
      "    # Mostrem els resultats per pantalla\n",
      "    print_results(data[item],temp_badget,invested_money,non_strategy)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}